﻿using System;
using System.Runtime.InteropServices;
using WindowsAPI.Windows;

namespace WindowsAPI.NativeControls
{
    public static class NativeListView
    {
        public static int GetCount(IntPtr Handle)
        {
            return User32.SendMessage(Handle, (WM)LVM.GETITEMCOUNT, IntPtr.Zero, IntPtr.Zero).ToInt32();
        }

        public static void SetText(IntPtr Handle, int Row, int Col, string Text)
        {
            var lvitem = new LV_ITEM
            {
                pszText = Text,
                iItem = Row,
                iSubItem = Col,
                mask = LVIF.TEXT
            };
            var buffer = Marshal.AllocHGlobal(Marshal.SizeOf(lvitem));
            Marshal.StructureToPtr(lvitem, buffer, true);
            User32.SendMessage(Handle, (WM)LVM.SETITEM, IntPtr.Zero, buffer);
            Marshal.FreeHGlobal(buffer);
        }

        public static string GetText(IntPtr Handle, int Row, int Col)
        {
            var lvitem = new LV_ITEM
            {
                cchTextMax = Consts.MAX_PATH,
                iItem = Row,
                iSubItem = Col,
                mask = LVIF.TEXT
            };
            User32.SendMessage(Handle, LVM.GETITEM, IntPtr.Zero, lvitem);
            return lvitem.pszText;
        }
    }
}
